package com.pb.camera.work;


import com.pb.camera.utils.TaskHelper;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/3/25 0025.
 */
public class HttpUtils {
    private Request request;
    private HttpUtilsHelper helper;
    public HttpUtils(){
        helper = HttpUtilsHelper.getHelper();
    }
    public void send(HttpRequest.HttpMethod method,String url, final RequestCallBack callBack){
        if (method == HttpRequest.HttpMethod.GET){
            methodGet(url,callBack);
        }
    }

    private void methodGet(String url , final RequestCallBack callBack){
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = helper.client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.onFailure(new HttpException(e),e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                callBack.onSuccess(new ResponseInfo(response.body().string()));
            }
        });
    }
}
